from tkinter import *
from middleWare import Middleware
from history_info import ScrollTable
from history import AttendanceDetailsApp


class HistoryWindow(Frame):
    def __init__(self, master):
        self.master = master

    def window(self):
        #设置一个Label
        self.text = Label(self.master, text="历史记录",bg='#fafafa',font=("微软雅黑", 20))
        

        #设置一个空区域，颜色为red
        self.line = Label(self.master,text="")
        self.line.config(width=67, height=6, bg='#ffffff')

        #读取照片
        self.photo = PhotoImage(file='static/请假记录.png')
        #将照片设置为按钮
        self.btn = Button(self.master, image=self.photo,relief='flat')#切换菜单
        #配置按钮宽高
        self.btn.config(width=60, height=60,bg='#ffffff',command=lambda:self.click('请假'))

        #读取照片作为按钮
        self.photo2 = PhotoImage(file='static/考勤记录_未选中.png')
        self.btn2 = Button(self.master, image=self.photo2, relief='flat')
        self.btn2.config(width=60, height=60, bg='#ffffff',command=lambda:self.click('考勤'))

        #设置一个文本域，文本为：请假
        self.text2 = Label(self.master, text="请  假", bg='#ffffff', font=("微软雅黑", 12))
        #文本颜色为#3b80f9
        self.text2.config(fg='#3b80f9')

        #设置一个文本域，文本为：考勤
        self.text3 = Label(self.master, text="考  勤", bg='#ffffff', font=("微软雅黑", 12))
        self.text3.config(fg='#929497')

        # #读取图片
        # self.photo3 = PhotoImage(file='static/空.png')
        # #创建一个区域显示图片
        # self.line2 = Label(self.master, image=self.photo3, bg='#ffffff')
        # self.line2.config(width=200, height=200, bg='#fafafa')

        #创建一个循环用于展示请假历史
        self.lis = [] 
        self.gethis()
        # print(lis)
        self.show_his()
        
        self.showat()
        self.getrevise()
        self.showlev()
    
    #展示考勤信息
    def showat(self):

        self.label1 = Label(self.master,text="",bg="#ffffff")
        self.label1.config(width=67,height=8)
        self.label2 = Label(self.master,text="————————————————————————————————————————————————",bg="#ffffff",fg='black')
        self.label2.config(width=67)
        self.label3 = Label(self.master,text="应到",bg='#ffffff')
        self.label3.config(font=("宋体",17),fg="#3b80f9")
        self.label4 = Label(self.master,text="实到",bg='#ffffff')
        self.label4.config(font=("宋体",17),fg="#3b80f9")
        self.label5 = Label(self.master,text="缺勤",bg='#ffffff')
        self.label5.config(font=("宋体",17),fg="#3b80f9")
        self.label6 = Label(self.master,text="2",bg='#ffffff')
        self.label6.config(font=("宋体",13),fg="#3b80f9")
        self.label7 = Label(self.master,text="2",bg='#ffffff')
        self.label7.config(font=("宋体",13),fg="#3b80f9")
        self.label8 = Label(self.master,text="0",bg='#ffffff')
        self.label8.config(font=("宋体",13),fg="#3b80f9")
        self.label9 = Label(self.master,text="缺旷人员",bg='#ffffff')
        self.label9.config(font=("宋体",15))

        #读取图片
        self.photo_ = PhotoImage(file='static/空.png')
        #创建一个区域显示图片
        self.line2_ = Label(self.master, image=self.photo_, bg='#fafafa')
        self.line2_.config(width=200, height=200)

        self.bnt_h = Button(self.master,text=".  .  .",bg="#fafafa",width=10, height=1, relief='flat',activebackground="#fafafa", activeforeground="white", bd=0, highlightthickness=0,font=("微软雅黑", 12))
        self.bnt_h.config(command=self.show_r_h)

    def showlev(self):
        self.lis_l = []
        self.lis_1 = []
        self.lis_2 = []
        self.lis_3 = []
        index=0
        for i in self.leave_lis:
            # print(i)
            if index<2:
                self.label_1 = Label(self.master,text=" ",bg="#ffffff",fg="#3b80f9",width=67,height=3)
                self.label_2 = Label(self.master,text='姓名',bg="#ffffff",fg='black')
                self.label_3 = Label(self.master,text=i,bg='#ffffff',fg='#3b80f9',font=("微软雅黑",15))
                self.label_4 = Label(self.master,text="未到",bg="#ffffff",fg='#3b80f9',font=("微软雅黑",15))
                self.lis_l.append(self.label_1)
                self.lis_1.append(self.label_2)
                self.lis_2.append(self.label_3)
                self.lis_3.append(self.label_4)
            index+=1


    def hidden(self):
        for i in self.lis_l:
            i.place_forget()
        for i in self.lis_1:
            i.place_forget()
        for i in self.lis_2:
            i.place_forget()
        for i in self.lis_3:
            i.place_forget()
    
    def show(self):
        index=0
        for i in self.lis_l:
            i.place(x=90,y=380+(index*70))
            index+=1
        for i in self.lis_1:
            i.place(x=110,y=270+(index*70))
            index+=1
        for i in self.lis_2:
            i.place(x=140,y=120+(index*70))
            index+=1
        for i in self.lis_3:
            i.place(x=480,y=-15+(index*70))
            index+=1

    def getrevise(self):
        mid = Middleware()
        ans = mid.get_new_his()
        dic = ans[0]
        self.leave_lis = ans[-1]
        self.label6.config(text=dic['all'])
        self.label7.config(text=dic['t'])
        self.label8.config(text=dic['leave'])

    def hidden_showat(self):
        self.label1.place_forget()
        self.label2.place_forget()
        self.label3.place_forget()
        self.label3.place_forget()
        self.label4.place_forget()
        self.label5.place_forget()
        self.label6.place_forget()
        self.label7.place_forget()
        self.label8.place_forget()
        self.label9.place_forget()
        self.line2_.place_forget()
        self.label_1.place_forget()
        self.label_2.place_forget()
        self.label_3.place_forget()
        self.label_4.place_forget()
        self.hidden()
        self.bnt_h.place_forget()
    
    def show_r_h(self):
        window = Toplevel(self.master)
        app = AttendanceDetailsApp(window)
        app.run()


    def un_hidden_showat(self):
        self.label1.place(x=90,y=230)
        self.label2.place(x=90,y=300)
        self.label3.place(x=160,y=240)
        self.label4.place(x=300,y=240)
        self.label5.place(x=440,y=240)
        self.label6.place(x=175,y=280)
        self.label7.place(x=315,y=280)
        self.label8.place(x=455,y=280)
        self.label9.place(x=110,y=330)
        if len(self.leave_lis) == 0:
            self.line2_.place(x=200,y=370)
        else:
            if len(self.leave_lis) == 1:
                self.label_1.place(x=90,y=380)
                self.label_2.place(x=110,y=400)
                self.label_3.place(x=150,y=390)
                self.label_4.place(x=480,y=390)
            else:
                self.show()
        self.bnt_h.place(x=260, y=540)




    def gethis(self):
        mid = Middleware()
        his = mid.data_binding_user_leave()
        self.lis = his

    

    #设置循环展示历史
    def show_his(self):
        lis = self.lis
        length = len(self.lis)
        if length != 0 :
            # self.hidden_empty()
            self.hid = []
            self.hid2 = []
            self.hid3 = []
            self.hid4 = []
            self.hid5 = []
            self.hid6 = []
            self.hid7 = []
            self.hid8 = []
            self.hid9 = []
            self.hid10 = []
            self.hid11 = []
            self.hid12 = []
            
            for i in range(length):
                if i<=2:
                    #设置一个文本域，文本为：“ ”
                    self.text4 = Label(self.master, text=" ", bg='#ffffff', font=("微软雅黑", 12))
                    self.text4.configure(width=47,height=4)
                    self.text4.place(x=90, y=140+(i+1)*100)
                    self.hid.append(self.text4)

                    #设置一个文本域，文本为：“ ”
                    self.text5 = Label(self.master, text="创建时间", bg='#ffffff', font=("微软雅黑", 8))
                    # self.text5.configure(width=47,height=4)
                    self.text5.place(x=100, y=145+(i+1)*100)
                    self.hid2.append(self.text5)

                    #设置一个文本域，文本为：“ ”
                    self.text6 = Label(self.master, text=lis[i]['time'], bg='#ffffff', font=("微软雅黑", 9),fg='#3b80f9')
                    # self.text6.configure(width=47,height=4)
                    self.text6.place(x=160, y=145+(i+1)*100)
                    self.hid3.append(self.text6)

                    #设置一个文本域，文本为：“ ”
                    self.text7 = Label(self.master, text=lis[i]['username'], bg='#ffffff', font=("微软雅黑", 8))
                    # self.text7.configure(width=47,height=4)
                    self.text7.place(x=500, y=145+(i+1)*100)
                    self.hid4.append(self.text7)


                    #设置一个线条
                    self.line_his = Canvas(self.master, width=460, height=2, bg="#e6e6e6", bd=0, highlightthickness=0)
                    self.line_his.place(x=100, y=165+(i+1)*100)
                    self.hid5.append(self.line_his)

                    #设置一个文本域，文本为：“ ”
                    self.text8 = Label(self.master, text="请假人", bg='#ffffff', font=("微软雅黑", 8),fg='black')
                    # self.text8.configure(width=47,height=4)
                    self.text8.place(x=100, y=195+(i+1)*100)
                    self.hid6.append(self.text8)

                    #设置一个文本域，文本为：“ ”
                    self.text9 = Label(self.master, text=lis[i]['leave_user'], bg='#ffffff', font=("微软雅黑", 11),fg='#3b80f9')
                    # self.text9.configure(width=47,height=4)
                    self.text9.place(x=140, y=191+(i+1)*100)
                    self.hid7.append(self.text9)

                    #设置一个文本域，文本为：“ ”
                    self.text10 = Label(self.master, text="时长", bg='#ffffff', font=("微软雅黑", 8),fg='black')
                    # self.text10.configure(width=47,height=4)
                    self.text10.place(x=225, y=195+(i+1)*100)
                    self.hid8.append(self.text10)

                    #设置一个文本域，文本为：“ ”
                    self.text11 = Label(self.master, text=str(lis[i]['leave_day'])+'天', bg='#ffffff', font=("微软雅黑", 11),fg='#3b80f9')
                    # self.text11.configure(width=47,height=4)
                    self.text11.place(x=250, y=191+(i+1)*100)
                    self.hid9.append(self.text11)

                    #设置一个文本
                    self.text12 = Label(self.master, text="描述", bg='#ffffff', font=("微软雅黑", 8),fg='black')
                    self.text12.place(x=335, y=195+(i+1)*100)
                    self.hid10.append(self.text12)

                    #设置一个文本域，文本为：“ ”
                    self.text13 = Label(self.master, text=lis[i]['leave_desc'], bg='#ffffff', font=("微软雅黑", 11),fg='#3b80f9')
                    self.text13.place(x=360, y=191+(i+1)*100)
                    self.hid11.append(self.text13)
            if length >= 3:
                #设置一个按钮
                self.btn_show = Button(self.master, text=".  .  .",bg="#fafafa" ,fg='black', font=("微软雅黑", 12))
                self.btn_show.config(width=10, height=1, relief='flat',activebackground="#fafafa", activeforeground="white", bd=0, highlightthickness=0)
                self.btn_show.config(command=self.show_more)
        # self.master.after(1000,self.show_his)
            
    def show_more(self):
        ScrollTable()

    
    def hidden_r(self):
        for i in self.hid:
            i.place_forget()
        for i in self.hid2:
            i.place_forget()
        for i in self.hid3:
            i.place_forget()
        for i in self.hid4:
            i.place_forget()
        for i in self.hid5:
            i.place_forget()
        for i in self.hid6:
            i.place_forget()
        for i in self.hid7:
            i.place_forget()
        for i in self.hid8:
            i.place_forget()
        for i in self.hid9:
            i.place_forget()
        for i in self.hid10:
            i.place_forget()
        for i in self.hid11:
            i.place_forget()
    
    def un_hidden_r(self):
        for i in self.hid:
            i.place(x=90, y=140+(self.hid.index(i)+1)*100)
        for i in self.hid2:
            i.place(x=100, y=145+(self.hid2.index(i)+1)*100)
        for i in self.hid3:
            i.place(x=160, y=145+(self.hid3.index(i)+1)*100)
        for i in self.hid4:
            i.place(x=500, y=145+(self.hid4.index(i)+1)*100)
        for i in self.hid5:
            i.place(x=100, y=165+(self.hid5.index(i)+1)*100)
        for i in self.hid6:
            i.place(x=100, y=195+(self.hid6.index(i)+1)*100)
        for i in self.hid7:
            i.place(x=140, y=191+(self.hid7.index(i)+1)*100)
        for i in self.hid8:
            i.place(x=225, y=195+(self.hid8.index(i)+1)*100)
        for i in self.hid9:
            i.place(x=250, y=191+(self.hid9.index(i)+1)*100)
        for i in self.hid10:
            i.place(x=335, y=195+(self.hid10.index(i)+1)*100)
        for i in self.hid11:
            i.place(x=360, y=191+(self.hid11.index(i)+1)*100)
        self.btn_show.place(x=260, y=540)
        
            
    # def hidden_empty(self):
    #     self.line2.place_forget()

    # def un_hidden_empty(self):
    #     self.line2.place(x=200, y=260)

        
    def click(self,name):
        self.photo_t = PhotoImage(file='static/请假记录_未选中.png')
        self.photo_t2 = PhotoImage(file='static/考勤记录.png')
        if name == '请假':
            self.btn.config(image=self.photo)
            self.btn2.config(image=self.photo2)
            self.text3.config(fg='#929497')
            self.text2.config(fg='#3b80f9')
            self.gethis()
            self.un_hidden_r()
            self.hidden_showat()
        elif name == '考勤':
            self.btn.config(image=self.photo_t)
            self.btn2.config(image=self.photo_t2)
            self.text2.config(fg='#929497')
            self.text3.config(fg='#3b80f9')
            # self.un_hidden_empty()
            self.hidden_r()
            self.un_hidden_showat()
            try:
                self.btn_show.place_forget()
            except:
                pass


    def hidden_window(self):
        self.text.place_forget()
        self.btn.place_forget()
        self.btn2.place_forget()
        self.line.place_forget()
        self.text2.place_forget()
        self.text3.place_forget()
        # self.line2.place_forget()
        self.hidden_r()
        self.hidden_showat()
        try:
            self.btn_show.place_forget()
        except:
            pass

    def un_hidden_window(self):
        self.text.place(x=100, y=50)
        self.btn.place(x=200, y=120)
        self.btn2.place(x=380, y=120)
        self.line.place(x=90, y=110)
        self.text2.place(x=209, y=180)
        self.text3.place(x=389, y=180)
        # self.line2.place(x=200, y=260)
        self.un_hidden_r()
        try:
            self.btn_show.place(x=260, y=540) 
        except:
            pass


